//
//  ForgetPayPWCardListController.m
//  HuLaQuan
//
//  Created by hok on 1/29/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "ForgetPayPWCardListController.h"


// Cells
#import "ForgetPayPWCardCell.h"
#import "ConfirmButtonCell.h"

//labels
#import "YuUserCellLabel.h"

//Logic Model
#import "GetUserAccountInfoEngine.h"
#import "GetUserDetailInfoEngine.h"


//Jump to
#import "ForgetPayPWCheckCardInfoController.h"
//Back To
#import "UserSecurityViewController.h"

@interface ForgetPayPWCardListController ()

@property (strong,nonatomic) NSMutableArray *cardList;

@property (nonatomic,strong) GetUserAccountInfoEngine *getUserAccountInfoEngine;
@property (nonatomic,strong) GetUserDetailInfoEngine *getUserInfoEngine;

@end

static NSString * const ForgetPayPWCardCellIdentifier = @"ForgetPayPWCardCell";
static NSString * const ConfirmButtonCellIdentifier = @"ConfirmButtonCell";

@implementation ForgetPayPWCardListController

- (GetUserAccountInfoEngine *)getUserAccountInfoEngine
{
    if (!_getUserAccountInfoEngine) {
        _getUserAccountInfoEngine = [[GetUserAccountInfoEngine alloc] init];
    }
    return _getUserAccountInfoEngine;
}
- (GetUserDetailInfoEngine *)getUserInfoEngine
{
    if (!_getUserInfoEngine) {
        _getUserInfoEngine = [[GetUserDetailInfoEngine alloc] init];
    }
    return _getUserInfoEngine;
}
- (NSMutableArray *)cardList
{
    if (!_cardList) {
        _cardList = [[NSMutableArray alloc] init];
    }
    return _cardList;
    
}
- (void)setupUI
{
    [self setupNavBackButton];
    
    self.tableView.backgroundColor = UIColorFromRGBA(kBackgroundColor, 1.0);
    
    
    [self setupFooterForTableView];
    
    [self.tableView registerNib:[ForgetPayPWCardCell nib] forCellReuseIdentifier:ForgetPayPWCardCellIdentifier];
    [self.tableView registerNib:[ConfirmButtonCell nib] forCellReuseIdentifier:ConfirmButtonCellIdentifier];
    
    
    
    self.title = @"验证银行卡";
    
    //[self setupTapGesture];
    

}

- (void)updateTableContent
{
    
    [self.tableView reloadData];
}

#pragma mark UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return kNormalCellHeight;

    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  
    if (self.cardList.count) {
        if (section == 0) {
            return self.cardList.count;
        }else{
            return 1;
        }
    }else{
        return 1;
    }
   
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.cardList.count ? 2 : 1 ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;

    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{
    if (self.cardList.count) {
        if (section == 0) {
            CGRect frameRect = CGRectMake(25, 0, 200, 40);
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
            YuUserCellLabel *textSection = [[YuUserCellLabel alloc] initWithFrame:frameRect];
            textSection.text = @"选择银行卡验证";
            [view addSubview:textSection];
            return view;
        }else{
            CGRect frameRect = CGRectMake(25, 0, 200, 40);
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
            YuUserCellLabel *textSection = [[YuUserCellLabel alloc] initWithFrame:frameRect];
            textSection.text = @"绑定新卡找回";
            [view addSubview:textSection];
            return view;
        }
    }else{
        CGRect frameRect = CGRectMake(25, 0, 200, 40);
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
        YuUserCellLabel *textSection = [[YuUserCellLabel alloc] initWithFrame:frameRect];
        textSection.text = @"绑定新卡找回";
        [view addSubview:textSection];
        return view;
    }
    

    
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
        ForgetPayPWCardCell *cell = [tableView dequeueReusableCellWithIdentifier:ForgetPayPWCardCellIdentifier
                                                               forIndexPath:indexPath];
        if(self.cardList.count)
        {
            if (indexPath.section == 0) {
                cell.titleLabel.text = self.cardList[indexPath.row][@"cardId"];
                
            }else{
                cell.titleLabel.text = @"添加新卡验证";
            }
            
        }else{
            cell.titleLabel.text = @"添加新卡验证";
        }
        return cell;
        
    
    
    
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    if(self.cardList.count && indexPath.section == 0)
    {
        self.userInfoAndSMS[@"card"] =self.cardList[indexPath.row];
    }
    
    
    [YuNetworkStatusUtilities showProgressMessage:@""];
    [self.getUserInfoEngine getUserInfo];

    
    NSLog(@"%@",self.userInfoAndSMS[@"tokenValue"]);
    //[self.getUserInfoEngine getUserInfoWithSMSToken:self.userInfoAndSMS[@"tokenValue"]];
    //[self performSegueWithIdentifier:@"ShowBankCardInfoFromList" sender:self];
    
}

#pragma mark - Navigation Back
- (void)goBackAction
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    UIAlertController *x = [UIAlertController alertWithTitle:@"提示" andMessage:@"是否放弃忘记支付密码操作" andBlock:^(UIAlertAction *action ) {
        if ([action.title isEqualToString:@"好的"]) {
            if ([self.navigationController.viewControllers[1] isKindOfClass:[UserSecurityViewController class]]) {
                [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
            }
        }
        
    }  type:@"confirmAndCancel"];
    [self presentViewController:x animated:YES completion:nil];
    
}
#pragma mark - Call Back
- (void)getUserInfoFinish:(NSNotification*) notification
{
    
    [YuNetworkStatusUtilities dismissHUD];
    
    NSDictionary *messageDic = [notification object];//获取到传递的对象
    if ([messageDic[@"isSuccess"] boolValue]) {
        if (messageDic[@"result"] && ![messageDic[@"result"][@"realName"] isEqualToString:@"Anonymous"]) {
            self.userInfoAndSMS[@"realName"] = messageDic[@"result"][@"realName"];
            
            
        }else{
            
            NSLog(@"self.userInfoAndSMS %@",self.userInfoAndSMS);
        }
        [self performSegueWithIdentifier:@"ShowCheckInfoFromForgetPayPWCardList" sender:self];
    }else{
        [self showNetWorkError:messageDic[@"message"]];
        
    }
}
/**
 *  获取用户银行卡信息后，刷新银行卡列表
 *
 *  @param notification
 */
- (void)getUserAccountInfoFinish:(NSNotification*) notification
{
    [YuNetworkStatusUtilities dismissHUD];
    NSDictionary *messageDic = [notification object];//获取到传递的对象
    if ([messageDic[@"isSuccess"] boolValue]) {
        if (messageDic[@"result"]) {
            
          //  self.passwordInfo = messageDic[@"result"];
            if (messageDic[@"result"][@"cardList"] ) {
              //  [self.cardList arrayByAddingObjectsFromArray:messageDic[@"result"][@"cardList"]];
                self.cardList = [[NSMutableArray alloc] initWithArray:messageDic[@"result"][@"cardList"] ];
            }
            NSLog(@"%@ class %@ self.cardList %@",messageDic[@"result"][@"cardList"],[messageDic[@"result"][@"cardList"] class],self.cardList);
            
            [self updateTableContent];
        }
    }else{
        [self showNetWorkError:messageDic[@"message"]];
        
    }
    
}

#pragma mark - Life Cycle
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getUserAccountInfoFinish:) name:@"GetUserAccountInfoFinish" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getUserInfoFinish:) name:@"GetUserInfoFinish" object:nil];
    
    NSLog(@"user info and sms %@",self.userInfoAndSMS);
    [YuNetworkStatusUtilities showProgressMessage:NETWORK_LOADING];
    [self.getUserAccountInfoEngine getAccoutInfo];
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"GetUserAccountInfoFinish" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"GetUserInfoFinish" object:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if([segue.identifier isEqualToString:@"ShowCheckInfoFromForgetPayPWCardList"])
    {
        ForgetPayPWCheckCardInfoController *xCon = (ForgetPayPWCheckCardInfoController *) [segue destinationViewController];
        NSLog(@"%@",self.userInfoAndSMS);
        xCon.userCardInfoAndSMSToken = self.userInfoAndSMS;
    }
}


@end
